package com.lyn.advance.adapters;

import android.annotation.SuppressLint;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import com.lyn.advance.fragments.PictureFragment;
import com.lyn.advance.fragments.VideoFragment;
import com.lyn.advance.interfaces.Advance;

import java.util.ArrayList;
import java.util.List;

public class AdvanceAdapter extends FragmentStateAdapter {

    private final List<AdvanceItem> advanceItems = new ArrayList<>();

    public AdvanceAdapter(@NonNull FragmentActivity fragmentActivity) {
        super(fragmentActivity);
    }

    public AdvanceAdapter(@NonNull Fragment fragment) {
        super(fragment);
    }

    public AdvanceAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
        super(fragmentManager, lifecycle);
    }

    @SuppressLint("NotifyDataSetChanged")
    public void clear(){
        advanceItems.clear();
        notifyDataSetChanged();
    }

    @SuppressLint("NotifyDataSetChanged")
    public void addFragment(AdvanceItem advanceItem) {
        advanceItems.add(advanceItem);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        try {
            AdvanceItem advanceItem = advanceItems.get(position);
            Fragment fragment = advanceItem.getFragmentClass().newInstance();
            if (fragment instanceof VideoFragment){
                VideoFragment videoFragment = (VideoFragment) fragment;
                videoFragment.init(advanceItem.getPath());
                videoFragment.setIntervalTime(advanceItem.getIntervalTime());
                videoFragment.setAdvanceListener(advanceItem.advanceListener);
            }else if (fragment instanceof PictureFragment){
                PictureFragment pictureFragment = (PictureFragment) fragment;
                pictureFragment.init(advanceItem.getPath());
                pictureFragment.setIntervalTime(advanceItem.intervalTime);
                pictureFragment.setAdvanceListener(advanceItem.advanceListener);
            }
            return fragment;
        }catch (Throwable e){
            e.printStackTrace();
        }
        return new Fragment();
    }

    @Override
    public int getItemCount() {
        return advanceItems.size();
    }


    public AdvanceItem getItem(int position){
        if (advanceItems.isEmpty()){
            return null;
        }
        return advanceItems.get(position);
    }


    public static class AdvanceItem{
        private Class<?extends Fragment> fragmentClass;
        private String path;
        private long intervalTime;
        private Advance.AdvanceListener advanceListener;

        public Class<? extends Fragment> getFragmentClass() {
            return fragmentClass;
        }

        public void setFragmentClass(Class<? extends Fragment> fragmentClass) {
            this.fragmentClass = fragmentClass;
        }

        public String getPath() {
            return path;
        }

        public void setPath(String path) {
            this.path = path;
        }

        public long getIntervalTime() {
            return intervalTime;
        }

        public void setIntervalTime(long intervalTime) {
            this.intervalTime = intervalTime;
        }

        public Advance.AdvanceListener getAdvanceListener() {
            return advanceListener;
        }

        public void setAdvanceListener(Advance.AdvanceListener advanceListener) {
            this.advanceListener = advanceListener;
        }
    }
}
